//
//  XBShareQQTIMView.m
//  XBShareAPI
//
//  Created by admin on 2018/5/18.
//  Copyright © 2018年 肖世恒. All rights reserved.
//

#import "XBShareQQTIMView.h"

#define mViewHeight  150
#define mBaseViewHeight [UIScreen mainScreen].bounds.size.height
#define mBaseViewWidth  [UIScreen mainScreen].bounds.size.width


@implementation XBShareQQTIMViewModel : NSObject


+ (XBShareQQTIMViewModel *)setModelQQNum:(NSString *)qqNum
                              completion:(XBSocialRequestCompletionHandler)completion{
 
    
    return [XBShareQQTIMViewModel setModelQQNum:qqNum groupNum:nil groupKey:nil isQZ:NO messageObject:nil completion:completion];
    
}

+ (XBShareQQTIMViewModel *)setModelGroupNum:(NSString *)groupNum
                                   groupKey:(NSString *)groupKey
                                 completion:(XBSocialRequestCompletionHandler)completion{
    return [XBShareQQTIMViewModel setModelQQNum:nil groupNum:groupNum groupKey:groupKey isQZ:NO messageObject:nil completion:completion];
}

+ (XBShareQQTIMViewModel *)setModelIsQZ:(BOOL)isqz
                          messageObject:(XBSocialMessageObject *)messageObject
                             completion:(XBSocialRequestCompletionHandler)completion{
    return [XBShareQQTIMViewModel setModelQQNum:nil groupNum:nil groupKey:nil isQZ:isqz messageObject:messageObject completion:completion];
}


+ (XBShareQQTIMViewModel *)setModelQQNum:(NSString *)qqNum
                                groupNum:(NSString *)groupNum
                                groupKey:(NSString *)groupKey
                                    isQZ:(BOOL)isqz
                           messageObject:(XBSocialMessageObject *)messageObject
                              completion:(XBSocialRequestCompletionHandler)completion{
    
    XBShareQQTIMViewModel *model = [[XBShareQQTIMViewModel alloc]init];
    if(groupNum != nil && groupKey != nil){
        model.type = 1;
    }else if(qqNum != nil){
        model.type = 2;
    }else if(messageObject != nil){
        model.type = 3;
    }
    model.isQZ = isqz;
    model.completion = completion;
    model.qqNum= qqNum;
    model.groupNum = groupNum;
    model.groupKey = groupKey;
    model.messageObject = messageObject;
    return model;

}

@end


@interface XBShareQQTIMView ()

@property (nonatomic,weak) id<XBShareQQTIMViewDelegate> delegate;
///视图
@property (nonatomic ,strong) UIView *mView;
///黑色背景
@property (nonatomic ,strong) UIView *mBaseView;

///线条
@property (nonatomic ,strong) UIView *xiantiao;
///取消
@property (nonatomic,strong) UIButton *cancelButton;

@end


@implementation XBShareQQTIMView

+ (XBShareQQTIMView *)defaultManager{
    
    static XBShareQQTIMView * s_instance_dj_singleton = nil ;
    if (s_instance_dj_singleton == nil) {
        s_instance_dj_singleton = [[XBShareQQTIMView alloc] init];
    }
    return s_instance_dj_singleton;
}
- (id)init{
    
    if(self = [super init]){
        
        self.frame = CGRectMake(0, 0, mBaseViewWidth, mBaseViewHeight);
        [self addSubview:self.mBaseView]; ///背景
        [self addSubview:self.mView];     ///内容试图
        [self.mView addSubview:self.cancelButton];
        [self.mView addSubview:self.xiantiao];
        
        [@[@"QQ",@"TIM"] enumerateObjectsUsingBlock:^(NSString *string, NSUInteger idx, BOOL *stop){
            
            UIView * view  = [[UIView alloc]init];
            view.backgroundColor = [ UIColor whiteColor];
            view.frame = CGRectMake(mBaseViewWidth/2 * idx, 0, mBaseViewWidth/2, mViewHeight - 55);
            [self.mView addSubview:view];
            
            UIButton *button = [[UIButton alloc]init];
            button.tag = idx;
            button.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
            [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(selectButtonType:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:button];
            
            
            UIImageView *imageview = [[UIImageView alloc]init];
            imageview.contentMode = UIViewContentModeScaleAspectFit;
            imageview.frame = CGRectMake(0, 10, view.frame.size.width, view.frame.size.height - 40);
            imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"share_%@",string]];
            [view addSubview:imageview];
            
            
            UILabel *title = [[UILabel alloc]init];
            title.frame = CGRectMake(0, view.frame.size.height - 20, view.frame.size.width, 20);
            title.font = [UIFont systemFontOfSize:16];
            title.textColor = [UIColor grayColor];
            title.textAlignment = NSTextAlignmentCenter;
            title.text = string;
            [view addSubview:title];
            
            
        }];
        
    }
    return self;
    
}
- (void)showXBShareQQTIMView:(id<XBShareQQTIMViewDelegate>)delegate;
{
    self.delegate = delegate;
    [self showJobsChooseView];
}

- (void)selectButtonType:(UIButton *)button{
    
    [self setHideJobsChooseView];
    if(self.delegate && [self.delegate respondsToSelector:@selector(selectType:)]){
     
        [self.delegate selectType:button.tag==0?XBSocialPlatformType_QQ:XBSocialPlatformType_Tim];
    }

}


///通知出现
- (void)showJobsChooseView{
    
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.mBaseView.alpha = 0;
    [self.mView setFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.mView.frame.size.height)];
    [UIView animateWithDuration:0.3 animations:^{
        
        self.mBaseView.alpha = 0.5;
        [self.mView setFrame:CGRectMake(0, self.frame.size.height - self.mView.frame.size.height, self.frame.size.width, self.mView.frame.size.height)];
    }];
    
}
///通知隐藏

- (void)setHideJobsChooseView{
    
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.mBaseView.alpha = 0;
                         [self.mView setFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.mView.frame.size.height)];
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                     }];
}
/**
 *  点击透明处 取消
 */
-(void)bgTappedAction:(UITapGestureRecognizer *)tap
{
    [self setHideJobsChooseView];
}

#pragma mark get view


- (UIButton *)cancelButton{
    
    if(!_cancelButton){
        
        _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, mViewHeight - 50 , mBaseViewWidth, 50)];
        _cancelButton.backgroundColor = [UIColor whiteColor];
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(setHideJobsChooseView) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _cancelButton;
    
}
- (UIView *)xiantiao{
    
    if(!_xiantiao){
        
        _xiantiao = [[UIView alloc]initWithFrame:CGRectMake(0, mViewHeight - 50, mBaseViewWidth, 5)];
        _xiantiao.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        
    }
    return _xiantiao;
    
}


- (UIView *)mView{
    
    if(!_mView){
        
        _mView = [[UIView alloc]init];
        _mView.backgroundColor = [UIColor whiteColor];
        _mView.frame = CGRectMake(0, mBaseViewHeight, mBaseViewWidth, mViewHeight);
    }
    return _mView;
}
- (UIView *)mBaseView{
    
    if(!_mBaseView){
        
        _mBaseView = [[UIView alloc]init];
        _mBaseView .backgroundColor = [UIColor darkGrayColor];
        _mBaseView.frame = CGRectMake(0, 0, mBaseViewWidth, mBaseViewHeight);
        UITapGestureRecognizer *bgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgTappedAction:)];
        [_mBaseView addGestureRecognizer:bgTap];
        
    }
    return _mBaseView;
}

@end
